package com.chenyue.cm.api.news;

import com.chenyue.cm.auth.Login;
import com.chenyue.cm.auth.LoginUser;
import com.chenyue.cm.auth.token.LoginUserInfo;
import com.chenyue.cm.auth.token.RequestUtil;
import com.chenyue.cm.bean.Success;
import com.chenyue.cm.common.vo.Page;
import com.chenyue.cm.news.service.JdNewsCommentService;
import com.chenyue.cm.news.service.JdNewsService;
import com.chenyue.cm.news.vo.IsCollect;
import com.chenyue.cm.news.vo.NewInfoVo;
import com.chenyue.cm.news.vo.UserNewsComment;
import com.chenyue.cm.user.domain.JdAppUser;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: ljy
 * @Title: ApiNewsController
 * @Description: 新闻Api
 * @Date: 2021-07-22
 */
@RestController
@RequestMapping("/api/news")
@Api(value = "新闻api", tags = "新闻api")
@ApiSort(5)
public class ApiNewsController {

    private final JdNewsService newsService;
    private final JdNewsCommentService newsCommentService;

    public ApiNewsController(JdNewsService jdNewsService, JdNewsCommentService jdNewsCommentService) {
        this.newsService = jdNewsService;
        this.newsCommentService = jdNewsCommentService;
    }

    /**
     * 根据新闻id获取新闻详情
     *
     * @return Success
     */
    @PostMapping("/getNewsInfo")
    @ApiOperation(value = "单个新闻的内容", notes = "单个新闻的内容")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "新闻id", name = "newsId", dataType = "int", required = true),
    })
    public Success<NewInfoVo> getNewsInfo(
            Integer newsId, HttpServletRequest request
    ) {
        int userId = 0;
        try {
            final LoginUserInfo userInfo = RequestUtil.getUserInfoByRequest(request);
            userId = userInfo.getUserId();
        } catch (Exception e) {
            e.printStackTrace();
        }
        NewInfoVo vo = newsService.getNewsInfo(newsId, userId);
        return Success.success(vo);
    }

    /**
     * 根据新闻id获取新闻评论
     *
     * @param newsId 新闻id
     * @return Success
     */
    @PostMapping("/getNewsComments")
    @ApiOperation(value = "单个新闻的评论", notes = "单个新闻的评论")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "新闻id", name = "newsId", dataType = "int", required = true),
    })
    public Success<Page<List<UserNewsComment>>> getNewsComments(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer rows,
            Integer newsId
    ) {
        Page<List<UserNewsComment>> result = newsCommentService.getNewsComments(page, rows, newsId);
        return Success.success(result);
    }

    /**
     * 发布评论
     *
     * @param newsId  新闻id
     * @param content 评论
     * @return Success
     */
    @Login
    @PostMapping("/postComment")
    @ApiOperation(value = "对单个新闻发表评论", notes = "对单个新闻发表评论")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "新闻id", name = "newsId", dataType = "int", required = true),
            @ApiImplicitParam(value = "评论内容", name = "content", dataType = "String", required = true),
    })
    public Success<String> postComment(Integer newsId, @ApiIgnore @LoginUser JdAppUser user, String content) {
        int success = newsCommentService.postComment(newsId, Integer.valueOf(user.getId()), content);
        return success > 0 ? Success.success() : Success.error();
    }

    /**
     * 收藏新闻
     *
     * @return Success
     */
    @Login
    @PostMapping("/collectNews")
    @ApiOperation(value = "对单个新闻进行收藏", notes = "对单个新闻进行收藏")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "新闻id", name = "newsId", dataType = "int", required = true),
    })
    public Success<IsCollect> collectNews(Integer newsId, @ApiIgnore @LoginUser JdAppUser user) {
        return newsService.collectNews(newsId, Integer.valueOf(user.getId()));
    }
}
